import 'dart:math';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:smart_devices/form/device_add_form.dart';

class DeviceModifyLogic extends GetxController {
  late Rx<DeviceAddForm> form;

  RxList<XFile> imgs = RxList([]);
  RxList<PlatformFile> files = RxList([]);
  Rx<TextEditingController> buyUserController = Rx(TextEditingController());
  Rx<TextEditingController> buyDeptController = Rx(TextEditingController());
  Rx<TextEditingController> departmentController = Rx(TextEditingController());
  Rx<TextEditingController> userController = Rx(TextEditingController());
  Rx<TextEditingController> typeController = Rx(TextEditingController());
  Rx<TextEditingController> statusController = Rx(TextEditingController());
  Rx<TextEditingController> locationController = Rx(TextEditingController());
  Rx<TextEditingController> startDateController = Rx(TextEditingController());
  Rx<TextEditingController> orderController = Rx(TextEditingController());
  Rx<TextEditingController> supplierController = Rx(TextEditingController());
  Rx<TextEditingController> buyDateController = Rx(TextEditingController());
  Rx<TextEditingController> leaveFactoryDateController =
      Rx(TextEditingController());

  @override
  void onInit() {
    super.onInit();
    var r = Random();
    for (var i = 0; i < 5; i++) {
      imgs.add(XFile(
          'https://img1.baidu.com/it/u=3285409119,3504542108&fm=253&fmt=auto&app=120&f=JPEG?w=1063&h=709',
          length: r.nextInt(90000)));
    }
    files.add(PlatformFile(name: '文档.pdf', size: 25678));
    files.add(PlatformFile(name: '表格文档.xlsx', size: 25678));
    files.add(PlatformFile(name: '幻灯片文档.pptx', size: 25678));
    files.add(PlatformFile(name: '图片.png', size: 25678));
    files.add(PlatformFile(name: 'Word文档.docx', size: 25678));
    files.add(PlatformFile(name: '压缩文档.zip', size: 25678));
    form = Rx(Get.arguments ?? DeviceAddForm());
  }

  @override
  void onClose() {
    super.onClose();
    buyUserController.value.dispose();
    buyDeptController.value.dispose();
    departmentController.value.dispose();
    userController.value.dispose();
    typeController.value.dispose();
    statusController.value.dispose();
    locationController.value.dispose();
    startDateController.value.dispose();
    orderController.value.dispose();
    supplierController.value.dispose();
    buyDateController.value.dispose();
    leaveFactoryDateController.value.dispose();
  }

  void uploadFilePath(List<XFile> res) {
    imgs.addAll(res);
  }
}
